package com.example.wan1.view.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.activity.SimpleActivity;
import com.example.wan1.model.CoinBean;
import com.example.wan1.model.PersonBean;
import com.example.wan1.model.UserBean;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.StatusBarUtil;
import com.example.wan1.utils.ToastUtils;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.MainActivity;
import com.example.wan1.view.coin.LoginCoinContract;
import com.example.wan1.view.coin.LoginCoinPresenter;
import com.google.android.material.textfield.TextInputLayout;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;

import javax.inject.Inject;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterActivity extends SimpleActivity implements LoginContract.View, LoginCoinContract.View {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.search)
    ImageView mSearch;
    @BindView(R.id.register_user_et)
    EditText mRegisterUserEt;
    @BindView(R.id.register_user)
    TextInputLayout mRegisterUser;
    @BindView(R.id.register_pwd_et)
    EditText mRegisterPwdEt;
    @BindView(R.id.register_pwd)
    TextInputLayout mRegisterPwd;
    @BindView(R.id.register_repwd_et)
    EditText mRegisterRepwdEt;
    @BindView(R.id.register_repwd)
    TextInputLayout mRegisterRepwd;
    @BindView(R.id.register_btn)
    Button mRegisterBtn;
    @BindView(R.id.register_tbar)
    Toolbar mToolbar;

    @Inject
    LoginPresenter mPresenter;

    @Inject
    LoginCoinPresenter mLoginCoinPresenter;
    private CharSequence mResult;
    private UserBean mUserBean;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        initTbar();
        mSearch.setVisibility(View.GONE);
        mTitle.setText("注册");

        RxTextView.textChanges(mRegisterUserEt)
                .subscribe(charSequence -> {
                    if (charSequence.toString().length() > 0 && !TextUtils.isEmpty(mRegisterPwdEt.getText().toString())
                            && !TextUtils.isEmpty(mRegisterRepwdEt.getText().toString()))
                        mRegisterBtn.setEnabled(true);
                    else
                        mRegisterBtn.setEnabled(false);
                });
        RxTextView.textChanges(mRegisterPwdEt)
                .subscribe(charSequence -> {
                    if (charSequence.toString().length() > 0 && !TextUtils.isEmpty(mRegisterUserEt.getText().toString())
                            && !TextUtils.isEmpty(mRegisterRepwdEt.getText().toString()))
                        mRegisterBtn.setEnabled(true);
                    else
                        mRegisterBtn.setEnabled(false);
                });
        RxTextView.textChanges(mRegisterRepwdEt)
                .subscribe(charSequence -> {
                    if (charSequence.toString().length() > 0 && !TextUtils.isEmpty(mRegisterPwdEt.getText().toString())
                            && !TextUtils.isEmpty(mRegisterUserEt.getText().toString()))
                        mRegisterBtn.setEnabled(true);
                    else
                        mRegisterBtn.setEnabled(false);
                });


        RxView.clicks(mRegisterBtn)
                .subscribe(o -> {
                    if (TextUtils.equals(mRegisterPwdEt.getText().toString(), mRegisterRepwdEt.getText().toString())) {
                        mPresenter.register(mRegisterUserEt.getText().toString(),
                                mRegisterPwdEt.getText().toString(),
                                mRegisterRepwdEt.getText().toString());
                    } else
                        ToastUtils.showToast("两次输入的密码不一致!");
                });

        int color = ToolsUtils.getThemeColor(this);
        if (color != 0) {
            mToolbar.setBackgroundColor(color);
            StatusBarUtil.setColor(this, color, 0);
            mRegisterBtn.setBackgroundColor(color);
            mRegisterPwd.setPasswordVisibilityToggleTintList(ColorStateList.valueOf(color));
            mRegisterRepwd.setPasswordVisibilityToggleTintList(ColorStateList.valueOf(color));
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void loginSuccess(PersonBean user) {
        //需要继续去登录，放在p层执行
        mUserBean = new UserBean();
        mUserBean.setUsername(user.getUsername());
        mUserBean.setPassword(user.getPassword());

        mResult = TextUtils.concat(user.getUsername(), "-", user.getPassword(), "-");
        mLoginCoinPresenter.getselfCoin();
    }

    @Override
    public void loginFailure(String msg) {
        ToastUtils.showToast(msg);
    }

    private void initTbar() {
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void selfCoinSuccess(CoinBean data) {
        //需要去请求数据才能得到具体的排名和等级
        mUserBean.setCoinCount(data.getCoinCount());
        mUserBean.setLevel(data.getLevel());
        mUserBean.setRank(data.getRank());
        Log.i("aaa", "selfCoinSuccess: "+data.getCoinCount());
        Log.i("aaa", "selfCoinSuccess: "+data.getRank());
        Log.i("aaa", "selfCoinSuccess: "+data.getLevel());

//        CharSequence result = TextUtils.concat(mResult, data.getRank() + "", "-",
//                data.getCoinCount() + "", "-", data.getLevel() + "");
        //SPUtils.put(this, "user_info", result);
        //改成数据库的模式，保存用户信息，并且把登陆状态使用sp保存了一下，当然也可以通过UserManager的isLogin的方法进行判断
        UserManager.getInstance().save(mUserBean);
        //注册成功直接到首页去
        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
        finish();
    }

    @Override
    public void selfCoinFailure(String msg) {
        //do nothing
    }
}
